/**
 * Created by asus on 2017/5/8.
 */

//明星主页
$(function(){
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURI(unescape(r[2]));
        }
        return null;
    }
    var u_sid = iService.getQueryString("u_sid");
    var u_name = iService.getQueryString("u_name");

    $(".secret_btn").on("click",function(){
        window.location.href = "../Message/starSecret.html?u_sid="+u_sid+"&u_name="+u_name;
    });

    $(".leave_btn").on("click",function(){
        window.location.href = "../Message/starLeave.html?u_sid="+u_sid+"&u_name="+u_name;
    });

    //明星主页预告列表
    function get_guard(){
        var guard_param = {
            url:'Homes/advance',
            data:{
                suid:u_sid
            }
        };
        $.JsonRpc(guard_param,function(data){
            var guard_list = data.data.list;
            if(!guard_list.length){
                var nullTxt = '<p class="null-tips-wrap">当前没有预告</p>';
                $(".sp_advance_wra").html(nullTxt);
            }
            var tpl = "";
            $.each(guard_list,function(i){
                var guard_date = guard_list[i].l_date.split("-");
                var guard_time = guard_list[i].l_endtime.slice(0,5);
                var gift_txt;
                if(guard_list[i].l_gift==10&&guard_list[i].l_package==10){
                    gift_txt = "将有红包和礼品发放";
                } else if(guard_list[i].l_gift==10){
                    gift_txt = "将有礼品发放";
                } else if(guard_list[i].l_package==10){
                    gift_txt = "将有礼品发放";
                } else if(guard_list[i].l_gift==0&&guard_list[i].l_package==0){
                    gift_txt = "暂无礼品发放";
                }
                tpl +=
                    '<li class="clearfix">'+
                    '<img class="fl sp_star_pic" src="'+guard_list[i].s_image+'" alt="">'+
                    '<div class="fl sp_info">'+
                    '<h4 class="sp_star_name">'+guard_list[i].s_name+'</h4>'+
                    '<p class="sp_gift_info">'+gift_txt+'</p>'+
                    '<p class="sp_booked_num">'+guard_list[i].l_booked+'人已订阅</p>'+
                    '<div class="sp_start_time">'+
                    '<span class="sp_date">'+guard_date[1]+'月'+guard_date[2]+'日</span>'+
                    '<span class="sp_hour">'+guard_time+'开始</span>'+
                    '</div>'+
                    '</div>';

                if(guard_list[i].booked==20){
                    tpl +=
                        '<a href="javascript:void(0)" data-l_id="'+guard_list[i].l_id+'" class="fr viewPlace">订阅</a>';
                }else if(guard_list[i].booked==10){
                    tpl +=
                        '<a href="javascript:void(0)" data-l_id="'+guard_list[i].l_id+'" class="fr viewPlace viewPlace_add">已订阅</a>';
                }
                tpl +=
                    '</li>';
            });
            $(".sp_advance_wra").html(tpl);

            $(".viewPlace").on("click",function(){
                iService.book_Fun(this);
            });
            $(".viewPlace_add").hover(function(){
                $(this).html("取消订阅");
            },function(){
                $(this).html("已订阅");
            });
            //book_Fun
        });
    }
    //回放列表
    function get_playBack(){

    /*layui.use('flow', function(){
        var flow = layui.flow;
        //当你执行这样一个方法时，即对页面中的全部带有lay-src的img元素开启了懒加载（当然你也可以指定相关img）
        flow.lazyimg();
    });*/

    layui.use('flow', function(){
        var $ = layui.jquery; //不用额外加载jQuery，flow模块本身是有依赖jQuery的，直接用即可。
        var flow = layui.flow;
        flow.load({
        elem: '#spBackWra' //指定列表容器
        ,isAuto:false
        ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
        var lis = [];
        //以jQuery的Ajax请求为例，请求下一页数据（注意：page是从2开始返回）
        var playback_param = {
            url:'Homes/playBack',
            data:{
                suid:u_sid,
                page:page,
                amount:20
            }
        };
        $.JsonRpc(playback_param,function(data){
            var playback_data = data.data.list;
            /*resdata=[];
            for(i=0;i<10;i++){
            resdata[i] = {money:i}
            }*/
            layui.each(playback_data, function(index, item){
                var listBoxClass = "";
                if((index+1)%2==0){
                    listBoxClass = "tripleBox"
                }

                var tpl =
                    '<div class="listBox '+listBoxClass+'">'+
                    '<div class="listPic">'+
                    '<a href="#">'+
                    '<img src="'+playback_data[index].s_image+'" alt="">'+
                    '<p class="time">回放</p>'+
                    '<span class="play_layer"><i></i></span>'+
                    '</a>'+
                    '</div>'+
                    '<div class="list_user_info clearfix">'+
                    '<span class="fl list_user_name">'+playback_data[index].s_name+'</span>'+
                    '<p class="fr num">'+
                    '<span>'+playback_data[index].l_viewing+'</span>'+
                    '<i></i>'+
                    '</p>'+
                    '</div>'+
                    '</div>';
                lis.push(tpl);
                });

                next(lis.join(''), page < Math.ceil(data.data.num/20));


            var listPic = $("#spBackWra .listPic");
            listPic.hover(function(){
                $(this).find(".play_layer").stop().show();
                //$(this).find("img").animate({width:"340px"});
                $(this).find("img").addClass("big")
            },function(){
                $(this).find(".play_layer").stop().hide();
                //$(this).find("img").animate({width:"340px"});
                $(this).find("img").removeClass("big")

            });


        });
        }
        });
    });


       /* var playback_param = {
            url:'Homes/resume',
            data:{
                suid:u_sid,
                page:page,
                amount:10

            }
        };
        $.JsonRpc(playback_param,function(data){
            var data_playback = data.data.list;
            var tpl="";
            tpl +=
                '<div class="listBox">'+
                '<div class="listPic">'+
                '<a href="#">'+
                '<img src="/themes//Public/assets/img/star.jpg" alt="">'+
                '<p class="time">回放</p>'+
                '<span class="play_layer"><i></i></span>'+
                '</a>'+
                '</div>'+
                '<div class="list_user_info clearfix">'+
                '<span class="fl list_user_name">刘诗诗</span>'+
                '<p class="fr num">'+
                '<span>26657</span>'+
                '<i></i>'+
                '</p>'+
                '</div>'+
                '</div>';




        });*/
    }

    //红包
    function get_package(){
        var send_param = {
            url:'Records/sendList',
            data:{
                suid:u_sid,
                type:10,
                page:1
            }
        };
        $.JsonRpc(send_param,function(data){
            var send_data = data.data.list;
            var tpl="";
            if(!send_data.length){
                $(".sp_money_list").html('<p class="null-tips-wrap">当前没有发出的红包</p>');
                return false;
            }
            $.each(send_data,function(k){
                var img_bg='<img src="/themes/show/public/assets/img/logo.png" alt="">';
                if(send_data[k].l_image){
                    img_bg = '<img src="'+send_data[k].l_image+'" onerror="iService.imgNoFind(this)" alt="">';
                }
                tpl +=
                    '<li class="clearfix">'+
                    '<div class="sp_money_info fl">'+
                    '<p class="sp_money_date">'+send_data[k].i_createtime.slice(0,16).replace(/-/g,".").replace(/ /,"-")+'</p>'+
                    '<div class="sp_money_amount">'+
                    '<img src= "/themes/show/public/assets/img/icon_redPack.png" alt="">'+
                    '<span>'+Math.floor(send_data[k].money*100)/100+'L币</span>'+
                    '</div>'+
                    '</div>'+
                    '<a class="sp_star_play fr" href="##">'+
                    img_bg+
                    '<i></i>'+
                    '</a>'+
                    '</li>';
            });
            $(".sp_money_list").html(tpl);
        });
    }

    //礼品
    function get_gift(){
        var send_param = {
            url:'Records/sendList',
            data:{
                suid:u_sid,
                type:20,
                page:1
            }
        };
        $.JsonRpc(send_param,function(data){
            var send_data = data.data.list;
            if(!send_data.length){
                $(".sp_actual_list").html('<p class="null-tips-wrap">当前没有发出的红包</p>');
                return false;
            }
            var tpl="";
            $.each(send_data,function(k){
                var send_date = send_data[k].i_createtime.split("-");
                tpl +=
                    '<li class="clearfix">'+
                    '<div class="sp_actual_info fl">'+
                    '<p class="sp_actual_date">'+send_data[k].i_createtime.slice(0,16).replace(/-/,".").replace(/ /g,"-")+'</p>'+
                    '<div class="sp_actual_amount">'+
                    '<span class="sp_actual">'+send_data[k].g_name+'</span>'+
                    '<span>'+send_data[k].i_number+'</span>'+
                    '</div>'+
                    '</div>'+
                    '<a class="sp_star_play fr" href="##">'+
                    '<img src="'+send_data[k].l_image+'" onerror="iService.imgNoFind(this)" alt="">'+
                    '<i></i>'+
                    '</a>'+
                    '</li>';
            });
            $(".sp_actual_list").html(tpl);
        });
    }

    //明星送礼列表
    function get_send(){
        //Records/sendList
        //type为10代表红包，20代表礼品
        get_package();
        layui.use('element', function() {
            var element = layui.element();
            element.on('tab(tab_gift)', function(data){
                //console.log(data);
                switch(data.index)
                {
                    case 0:
                        get_package();
                        break;
                    case 1:
                        get_gift();
                        break;
                    default:
                        console.log("Error");
                }
            });
        });

    }
    //动态列表
    function get_circles(){
        layui.use('flow', function(){
            var $ = layui.jquery; //不用额外加载jQuery，flow模块本身是有依赖jQuery的，直接用即可。
            var flow = layui.flow;
            flow.load({
                elem: '.sp_dynamic_wra' //指定列表容器
                ,isAuto:false
                ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
                    var lis = [];
                    //以jQuery的Ajax请求为例，请求下一页数据（注意：page是从2开始返回）
                    var myCircle_param = {
                        url:'Circles/circleList',
                        data:{
                            page:page,
                            suid:0
                        }
                    };
                    $.JsonRpc(myCircle_param,function(data){
                        var star_list_data = data.data.list;
                        console.log(star_list_data);
                        layui.each(star_list_data, function(index, item){
                            /*var listBoxClass = "";
                             if((index+1)%4==0){
                             listBoxClass = "tripleBox"
                             }*/
                            var likeClassName = "";
                            (star_list_data[index].booked==10)?likeClassName = "active":likeClassName = "";
                            var creat_time = star_list_data[index].c_createtime.slice(0,15);
                            var img_all = "";
                            if(star_list_data[index].c_images) {
                                var img_arr = String(star_list_data[index].c_images).split(",");
                                for(var i=0;i<img_arr.length;i++){
                                    img_all += '<a href="../Circle/detail.html?cid='+star_list_data[index].cid+'"><img src="'+img_arr[i]+'" alt=""></a>';
                                }
                            }
                            var tpl =
                                '<div class="sc_list_box" id="cid'+star_list_data[index].cid+'">'+
                                '<div class="sc_title clearfix">'+
                                '<a href="../Page/index.html?u_sid='+star_list_data[index].c_udid+'"><img class="star_img fl" src="'+star_list_data[index].u_avatar+'" alt=""></a>'+
                                '<div class="fl">'+
                                '<h4 class="star_name">'+star_list_data[index].u_nickname+'</h4>'+
                                '<p class="star_publish_time">'+creat_time+'</p>'+
                                '</div>'+
                                '</div>'+
                                '<p class="star_publish_txt"><a href="../Circle/detail.html?cid='+star_list_data[index].cid+'">'+star_list_data[index].c_content+'</a></p>'+
                                '<div class="imgWra clearfix">'+
                                img_all+
                                '</div>'+
                                '<ul class="sc_row_line clearfix">'+
                                '<li>'+
                                '<span class="c_like">'+star_list_data[index].c_like+'</span>'+
                                '<i class="ic_praise '+likeClassName+'" data-cid="'+star_list_data[index].cid+'"></i>'+
                                '</li>'+
                                '<li>'+
                                '<span>'+star_list_data[index].c_comment+'</span>'+
                                '<i class="ic_comment"></i>'+
                                '</li>'+
                                '<li>'+
                                '<span>'+star_list_data[index].c_share+'</span>'+
                                '<i class="ic_share"></i>'+
                                '</li>'+
                                '</ul>'+
                                '</div>';
                            lis.push(tpl);
                        });
                        next(lis.join(''), page < Math.ceil(data.data.num/20));
                        //点赞帖子
                        $(".ic_praise").off("click").on("click",function(){
                            var ic_prize_now = $(this);
                            var cid = $(this).data("cid");
                            var praise_param = {
                                url:'Circles/likeCircle',
                                data:{
                                    cid: cid
                                }
                            };
                            $.JsonRpc(praise_param,function(data){
                                if(data.code==200){
                                    if(data.data.status==10){
                                        ic_prize_now.addClass('layui-anim layui-anim-scaleSpring active');
                                        ic_prize_now.siblings(".c_like").html(data.data.number);
                                    }else if(data.data.status==20){
                                        ic_prize_now.removeClass('layui-anim layui-anim-scaleSpring active');
                                        ic_prize_now.siblings(".c_like").html(data.data.number);
                                    }
                                }
                            })
                        });
                        $(".sg_list_wra").on("mouseover mouseout",".btn_cancel",function(event){
                            if(event.type == "mouseover"){
                                $(this).html("取消关注");
                            }else if(event.type == "mouseout"){
                                $(this).html("已关注");
                            }
                        });
                    });
                }
            });
        });
    }
    //明星档案
    function get_resume(){
        var resume_param = {
            url:'Homes/resume',
            data:{
                suid:u_sid
            }
        };
        $.JsonRpc(resume_param,function(data){
            var data_resume = data.data;
            var tpl = "";
            tpl +=
                '<li class="clearfix">'+
                '<span class="fl">中文名</span>'+
                '<p class="fl">'+data_resume.s_name+'</p>'+
                '</li>'+
                '<li class="clearfix">'+
                '<span class="fl">粉丝名</span>'+
                '<p class="fl">'+data_resume.u_nickname+'</p>'+
                '</li>'+
                '<li class="clearfix">'+
                '<span class="fl">星座</span>'+
                '<p class="fl">'+data_resume.s_astro+'</p>'+
                '</li>'+
                '<li class="clearfix">'+
                '<span class="fl">出生地</span>'+
                '<p class="fl">'+data_resume.s_born+'</p>'+
                '</li>'+
                '<li class="clearfix">'+
                '<span class="fl">代表作品</span>'+
                '<p class="fl">'+data_resume.s_works+'</p>'+
                '</li>'+
                '<li class="clearfix">'+
                '<span class="fl">主要成就</span>'+
                '<p class="fl">'+data_resume.s_record+
                '</p>'+
                '</li>';

            $(".sp_profile_wra").html(tpl);

        });
    }

    layui.use('element', function(){
        var element = layui.element();
        //一些事件监听
        element.on('tab(tab_star)', function(data){
            //console.log(this);
            //console.log(data);
            //Tab切换请求数据
            switch(data.index)
            {
                case 0:
                    get_guard();
                    break;
                case 1:
                    get_playBack();
                    break;
                case 2:
                    get_circles();
                    break;
                case 3:
                    get_send();
                    break;
                case 4:
                    get_resume();
                    break;
                default:
                    console.log("Error");
            }

        });
        element.on('tab(tab_gift)', function(data){
            console.log(data);
        });
    });


    //流加载html
    /*<ul id="demo">
    <li>1</li>
    </ul>*/
    //流加载js
    /*layui.use('flow', function(){
        var $ = layui.jquery; //不用额外加载jQuery，flow模块本身是有依赖jQuery的，直接用即可。
        var flow = layui.flow;
        flow.load({
            elem: '#demo' //指定列表容器
            ,isAuto:false
            ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页
                var lis = [];
                //以jQuery的Ajax请求为例，请求下一页数据（注意：page是从2开始返回）
                var guard_param = {
                    url:'Homes/guard',
                    data:{
                        suid:u_sid,
                        page:page
                    }
                };
                $.JsonRpc(guard_param,function(data){
                    console.log(data);
                    var resdata = data.data.list;
                    /!*resdata=[];
                    for(i=0;i<10;i++){
                        resdata[i] = {money:i}
                    }*!/

                    layui.each(resdata, function(index, item){
                        lis.push('<li>'+ item.money +'</li>');
                    });

                    next(lis.join(''), page < Math.ceil(data.num/data.page));
                });
            }
        });
    });*/

    var starPage_param = {
        url:'Homes/star',
        data:{
            suid:u_sid
        }
    };
    $.JsonRpc(starPage_param,function(data){
        console.log(data);
        var star_info = data.data.star;
        var tpl =
            '<img class="sp_back_pic" src="'+star_info.s_backdrop+'" alt="背景图片">'+
            '<span class="sp_shade"></span>'+
            '<span id="sp_lsid">LSID：'+data.content.suid+'</span>'+
            '<div id="sp_info_base">'+
            '<div id="sp_head_wra" class="fl">'+
            '<img class="sp_head" src="'+star_info.u_avatar+'" alt="">';

        if(star_info.booked==10){
            tpl+=
                '<a data-s_status="'+star_info.booked+'" data-sid = "'+star_info.s_udid+'" class="focus_btn btn_cancel fr" style="border:none;" href="javascript:void(0)">'+
                '已关注'+
                '</a>';
        }else if(star_info.booked==20){
            tpl+=
                '<a data-s_status="'+star_info.booked+'" data-sid = "'+star_info.s_udid+'" class="focus_btn btn_confirm fr" href="javascript:void(0)">'+
                '关注'+
                '</a>';
        }
        tpl +=
            //'<a class="focus_btn fr" href="javascript:void(0)"><i class="ic_follow"></i>关注</a>'+
            '</div>'+
            '<div class="sp_text_wra fl">'+
            '<h4 class="sp_name">'+star_info.s_name+'</h4>'+
            '<div class="sp_label">'+
            '<span>'+star_info.s_musictype +'</span>' +
            //'<span>演员</span>'+
            '</div>'+
            '<div class="sp_popular">'+
            //'<a data-s_status="20" data-sid="19" class="focus_btn btn_confirm fr" href="javascript:void(0)">关注</a>'+
            '<span>被关注：<i>'+star_info.s_booked+'</i></span><span>人气：<i>'+star_info.u_obtain+'</i></span>'+
            '</div>'+
            '</div>'+
            '</div>';
        $("#sp_info_wra").html(tpl);
        $("#sp_info_wra").on("mouseover mouseout",".btn_cancel",function(event){
            if(event.type == "mouseover"){
                $(this).html("取消关注");
            }else if(event.type == "mouseout"){
                $(this).html("已关注");
            }
        });
        $(".focus_btn").on("click",function(){
            iService.focus_Fun(this);
        });
        //守护列表
        var getGuardData = function(){
            var guard_param = {
                url:'Homes/guard',
                data:{
                    suid:u_sid,
                    page:1
                }
            };
            $.JsonRpc(guard_param,function(data){
                console.log(data);
                data = data.data.list;
                var le = data.length;
                var tpl1 = "";
                var tpl2 = "";
                var top_class_name,rank_num;
                $(".guardTitle  .tips ").html('(前'+le+'名)');
                if(le<=10){
                    $(".guardTitle .page_len ").html('1/1');
                    for(var i= 0;i<le;i++){
                        (i<3)?(top_class_name = "fans_top_three",rank_num=""):(top_class_name = "",rank_num=(parseInt(i)+1));
                        tpl1 +=
                            '<li>'+
                            '<span class="fl fans_rank fans_top'+(1+i)+' '+top_class_name+'">'+rank_num+'</span>'+
                            '<img class="fl fans_head" src="'+data[i].u_avatar+'" alt=""/>'+
                            '<h4 class="fl fans_name">'+data[i].u_nickname+'</h4><span class="user_leval_mark fans_leval user_leval_'+iService.get_user_leval(data[i].u_score)+'"></span>'+
                            '<span class="fr fans_lb_num">'+data[i].money+'L币</span>'+
                            '</li>';
                    }
                    $(".guardWrapper").html(tpl1);
                }else if(le>10){
                    $(".guardTitle .page_len ").html('1/2');
                    for(var k=0;k<le;k++){
                        (k<3)?top_class_name = "fans_top_three":top_class_name = "";
                        if(k<10){
                            tpl1 +=
                                '<li>'+
                                '<span class="fl fans_rank '+top_class_name+'">'+k+'</span>'+
                                '<img class="fl fans_head" src="'+data[k].u_avatar+'" alt=""/>'+
                                '<h4 class="fl fans_name">'+data[k].u_nickname+'</h4><span class="user_leval_mark fans_leval user_leval_'+iService.get_user_leval(data[k].u_score)+'"></span>'+
                                '<span class="fr fans_lb_num">'+data[k].money+'L币</span>'+
                                '</li>';
                        }else {
                            tpl2 +=
                                '<li>'+
                                '<span class="fl fans_rank">'+k+'</span>'+
                                '<img class="fl fans_head" src="'+data[k].u_avatar+'" alt=""/>'+
                                '<h4 class="fl fans_name">'+data[k].u_nickname+'</h4><span class="user_leval_mark fans_leval user_leval_'+iService.get_user_leval(data[k].u_score)+'"></span>'+
                                '<span class="fr fans_lb_num">'+data[k].money+'L币</span>'+
                                '</li>';
                        }
                    }
                    $(".guardWrapper").html(tpl1);
                    $(".page_next").on("click",function(){
                        $(".guardTitle .page_len ").html('2/2');
                        $(".guardWrapper").html(tpl2);
                    });
                    $(".page_prev").on("click",function(){
                        $(".guardTitle .page_len").html('1/2');
                        $(".guardWrapper").html(tpl1);
                    });
                }
                //预告默认
                get_guard();
            });
        };
        getGuardData();
    });
});